<?php
class JobProvidersController extends AppController {

	var $name = 'JobProviders';
	
	function beforeFilter(){
		if($this->Session->read('Auth.User')){
			$this->Auth->allow('*');
		}
	}
	
	function admin_index($id = NULL) {
		if($id != NULL){
			$this->JobProvider->recursive = 0;
			$this->set('jobProviders', $this->paginate(array('JobProvider.id' => $id)));
		}else{
			$this->JobProvider->recursive = 0;
			$this->set('jobProviders', $this->paginate());
		}
		
	}
	
	function admin_save(){
		$this->autoRender = false;
		if (!empty($this->data)) {
			if(isset($this->data['JobProvider']['location.id']) == NULL){
				unset($this->data['JobProvider']['location.id']);
			}
			$date = time();
			$this->data['JobProvider']['user_id'] = 1;
			$this->data['JobProvider']['updated'] = strftime("%y-%m-%d",$date);
			$this->JobProvider->create();
			if ($this->JobProvider->save($this->data)) {
				$this->Session->setFlash(__('The job provider has been saved', true));
				$this->redirect(array('action' => 'index',$this->JobProvider->id));
			}else{
				$this->Session->setFlash(__('The job provider could not be saved. Please, try again.', true));
				$jobLocations = $this->JobProvider->JobLocation->find('list', array('conditions' => array(
																						'JobLocation.parent_id' => NULL)));
				$this->set('jobLocations',$jobLocations);
				$this->render('admin_add');
			}
		}
	}

	function admin_add($id = NULL,$edit = NULL) {
		if($id != NULL){
			$this->data = $this->JobProvider->read(null, $id);
		}
		$jobLocations = $this->JobProvider->JobLocation->find('list', array('conditions' => array(
																						'JobLocation.parent_id' => NULL)));
		$this->set('jobLocations',$jobLocations);
	}

	function admin_delete($id = null) {
		if(($this->RequestHandler->isAjax())) {
			$this->autoRender = false;
			if (!$id) {
				return 'error';
			}
			if ($this->JobProvider->delete($id)) {
				return 'Success';
			}
			return 'error';
		}
	}
	
	function admin_export_companies(){
		$this->set('jobProviders', $this->paginate());
		$number = $this->JobProvider->find('all', array('fields' => 'count(*) as total'));
		$this->set('total', $number[0][0]);
		$this->render('admin_export_companies','export_companies');
	}
}
?>